package com.th.coupon.entity;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.th.coupon.common.vo.CouponTemplateSdk;
import com.th.coupon.constant.CouponStatus;
import com.th.coupon.converter.CouponStatusConverter;
import com.th.coupon.serialization.CouponSerialize;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.util.Date;

/**
 * 优惠卷实体
 * @author 涂航
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "coupon")
@JsonSerialize(using= CouponSerialize.class)
@EntityListeners(AuditingEntityListener.class)
public class Coupon {

    /**
     * 主键id
     */
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name = "id",nullable = false)
    private Integer id;

    /**
     * 模板id (逻辑外键)
     */
    @Column(name = "template_id",nullable = false)
    private Integer templateId;

    /**
     * 领取用户
     */
    @Column(name = "user_id",nullable = false)
    private Long userId;

    /**
     * 优惠卷码
     */
    @Column(name = "coupon_code",nullable = false)
    private String couponCode;

    /** 领取时间 */
    @CreatedDate
    @Column(name = "assign_time", nullable = false)
    private Date assignTime;

    /** 优惠券状态 */
    @Column(name = "status", nullable = false)
    @Convert(converter = CouponStatusConverter.class)
    private CouponStatus status;

    /** 用户优惠券对应的模板信息 */
    @Transient
    private CouponTemplateSdk templateSDK;

    /**
     * <h2>返回一个无效的 Coupon 对象</h2>
     * */
    public static Coupon invalidCoupon() {

        Coupon coupon = new Coupon();
        coupon.setId(-1);
        return coupon;
    }

    /**
     * <h2>构造优惠券</h2>
     * */
    public Coupon(Integer templateId, Long userId, String couponCode,
                  CouponStatus status) {
        this.templateId = templateId;
        this.userId = userId;
        this.couponCode = couponCode;
        this.status = status;
    }
}
